const waitDefault = 60
let timer = null

export default {
	state: {
		wait: localStorage.getItem('login-sms-verify-second') * 1
	},
	getters: {},
	mutations: {
		SET_WAIT(state, val) {
			state.wait = val
			localStorage.setItem('login-sms-verify-second', val)
		}
	},
	actions: {
		startWait({ commit, dispatch, state }) {
			commit('SET_WAIT', waitDefault)
			if (!timer) {
				dispatch('readSecond')
			}
		},
		readSecond({ commit, dispatch, state }) {
			clearInterval(timer)
			timer = setInterval(() => {
				commit('SET_WAIT', Math.max(0, state.wait - 1))
				if (state.wait < 1) {
					dispatch('endWait')
				}
			}, 1000)
		},

		endWait({ commit, state }) {
			clearInterval(timer)
			timer = null
		}
	},
	modules: {}
}
